const Sequelize = require('sequelize')
const db = require('./db')
const Article = require('./Article')
// 将allowNull设置为false会将NOT NULL添加到列中，
const Category = db.define(
  'category',
  {
    id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false },
    index: {type: Sequelize.STRING, allowNull: false, defaultValue: 0},
    name: { type: Sequelize.STRING, allowNull: false, defaultValue: '' },
    created_at: {
      type: Sequelize.DATE,
      defaultValue: Sequelize.NOW,
      allowNull: false
    },
    updated_at: {
      type: Sequelize.DATE,
      defaultValue: Sequelize.NOW,
      allowNull: false
    }
  },
  {
    // 如果为 true 则表的名称和 model 相同，即 user
    // 为 false MySQL创建的表名称会是复数 users
    // 如果指定的表名称本就是复数形式则不变
    // freezeTableName: true,
    timestamps: false,
    tableName: 'categorys'
  }
)
Category.hasMany(Article, { as: 'Articles', foreignKey: 'category_id' })

Category.sync({ force: false })
  .then(res => {
    console.log('create table successfully!')
  })
  .catch(err => {
    console.log('create table error!', err)
  })
module.exports = Category
